package com.china08.yxyapi.api.manage.tec.teaching.lecture.prepare;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;

/**
 * 老师 ---备授课--成品备课包详情--修改资源用途
 * 
 */
@CrossOrigin
@RestController
public class LectureUptPrePackUsedController extends YxyApiBaseController {

    @Autowired
    private YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/prepare/used/upt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req,
            RespModel respModel) {
        RespModel model = new RespModel();
        YxyLecturePrePack prePack = yxyLecturePrePackRepository.findById(req.prePackId);
        List<Res> resList = req.resources;
        List<com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res> list = prePack.getResources();
        // 先清空资源用途
        for (com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res res : list) {
            res.setUsesHomework(0);
            res.setUsesProview(0);
            res.setUsesTeaching(0);
            res.setUsesTest(0);
        }
        yxyLecturePrePackRepository.save(prePack);
        // 添加资源用途
        for (com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res res : list) {
            for (Res ress : resList) {
                if (res.getResourceId().equals(ress.resourceId)) {
                    List<Integer> resUsedList = ress.resUsed;
                    for (Integer used : resUsedList) {
                        if (5 != used) {
                            if (1 == used) {
                                res.setUsesTeaching(1);
                            }
                            if (2 == used) {
                                res.setUsesProview(1);
                            }
                            if (3 == used) {
                                res.setUsesHomework(1);
                            }
                            if (4 == used) {
                                res.setUsesTest(1);
                            }
                        }
                    }
                }
            }
        }
        yxyLecturePrePackRepository.save(prePack);
        model.setMessage("修改成功");
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String prePackId;
        /**
         * 资源id
         */
        public List<Res> resources;

    }

    protected static class Res {
        public String resourceId;
        // 资源用途 1 授课 2 预习作业 3 课后作业 4 测试
        public List<Integer> resUsed;
    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
